package com.exit66.jukebox;

import com.exit66.jukebox.data.Library;
import com.exit66.jukebox.data.Playlist;
import com.exit66.jukebox.data.RequestQueue;
import com.exit66.jukebox.data.Track;

public class DemoSoundPlayer extends Thread {
    private static final int STATUS_STOPPED = 0;
    private static final int STATUS_PLAYING = 1;
    private static final int STATUS_SHUTDOWN = 2;
    
    int currentStatus = STATUS_STOPPED;
	private int queuedTrackID = -1;
	
    private static DemoSoundPlayer ref;
    public int currentTrackID = -1;
    
    public static DemoSoundPlayer getSoundPlayer() {
        if (ref == null)
            ref = new DemoSoundPlayer();
        return ref;
    }
    
    public Object clone()
    throws CloneNotSupportedException {
        throw new CloneNotSupportedException();
    }
    
    private DemoSoundPlayer() {
    	currentStatus = STATUS_STOPPED;
        setName("Exit 66 Player");
        start();
    }
    
    public void run() {
        try {
            waitForNext();
        } catch(Exception exception) {
            System.out.println(exception);
        }
    }
    
    private void waitForNext() {
        try {
            while(true) {
                if(currentStatus != STATUS_STOPPED)
                    break;
                if(!nextSong())
                    Thread.sleep(5000L);
            }
            if(currentStatus == STATUS_SHUTDOWN)
                return;
        } catch(Exception exception) {
            System.err.println(exception);
        }
    }
    
    private boolean nextSong() {
        RequestQueue requestqueue = new RequestQueue();
        int i = requestqueue.getNextRequest();
        if(i == -1) {
            i = Playlist.getNextTrackID();
        }
        if(i != -1) {
            Track track = new Track();
            track.setTrackID(i);
            track.playTrack();
            track.fetch();
            if(track.getLibraryID() != -1) {
                Library library = new Library();
                library.setLibraryID(track.getLibraryID());
                library.fetch();
                if(track.getFileName().length() != 0 && library.getLibraryPath().length() != 0) {
                    queueSong(i, library.getLibraryPath().concat(track.getFileName()));
                    try {
                        Thread.sleep(1000L);
                    } catch(Exception exception) { }
                    return true;
                } else {
                	
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    
    private void playSong() {
    	try {
	    	DemoSoundPlayer.getSoundPlayer().currentTrackID = queuedTrackID;
			queuedTrackID = -1;
			currentStatus = STATUS_PLAYING;
			Thread.sleep(180000L);
			songComplete();
    	} catch(Exception exception) {
    		System.err.print(exception);
            currentStatus = STATUS_STOPPED;
            DemoSoundPlayer.getSoundPlayer().currentTrackID = -1;
        }
    }
    
    private void queueSong(int i, String s) {
        try {
        	queuedTrackID = i;
        	playSong();
        	
        } catch(Exception exception) {
            System.err.print(exception);
            currentStatus = STATUS_STOPPED;
            DemoSoundPlayer.getSoundPlayer().currentTrackID = -1;
        }
    }
    
    public void playPause() {

    }
        
    private void songComplete() {
    	System.out.println("done playing");
    	DemoSoundPlayer.getSoundPlayer().currentTrackID = -1;
        if (currentStatus != STATUS_SHUTDOWN) {
            currentStatus = STATUS_STOPPED;
        }
        waitForNext();
    }
    
    public void stopSong() {
        currentStatus = STATUS_STOPPED;
        songComplete();
    }
    
    public void shutDown() {
        currentStatus = STATUS_SHUTDOWN;
    }

}
